package com.example.demo;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class BCcrytPassEncoder {
    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String rawPassword = "test123"; // 明文

        // 加密后
        String encodedPassword = encoder.encode(rawPassword);
        System.out.println("Encoded Password: " + encodedPassword);
        //Encoded Password: $2a$10$xJzddcc6hH0Ysnbliag5iewLOKH3w1xQx/Y/5NKi4yh6hU.Sem1b2

        String encodedPassword2 = encoder.encode(rawPassword);
        System.out.println("Encoded Password: " + encodedPassword2);
        //Encoded Password: $2a$10$cWt9z1UwXbQXgbtMenSRf.d4vSfTVIhCOBHDCsvWVlr9z7aoA8ul6

        // 验证加密后的密文和明文是否匹配
        boolean isMatch = encoder.matches(rawPassword, encodedPassword);
        System.out.println("Password Match: " + isMatch);
        //Password Match: true

        isMatch = encoder.matches(rawPassword, encodedPassword2);
        System.out.println("Password Match: " + isMatch);
        //Password Match: true
    }
}
